package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author wuJiaWei
 * @version 1.0
 */
@RestController
public class LoginController {

    @Autowired
    private EmpService empService;

    /**
     * 登录接口
     * @param emp
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        Emp e = empService.login(emp);
        if (Objects.nonNull(e)){
            //自定义信息
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", e.getId());
            claims.put("username", e.getUsername());
            claims.put("name", e.getName());
            //使用JWT工具类,生成身份令牌
            String jwtToken = JwtUtils.generateJwt(claims);
            return Result.success(jwtToken);
        }else{
            return Result.error("用户名或密码错误");
        }
    }

}
